//
//  ViewController.m
//  VAPPlayer
//
//  Created by bj_szd on 2024/9/5.
//

#import "ViewController.h"
#import <QGVAPWrapView.h>

@interface ViewController ()<VAPWrapViewDelegate, HWDMP4PlayDelegate>

@property (nonatomic, strong) QGVAPWrapView *wrapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _wrapView = [[QGVAPWrapView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
    [self.view addSubview:_wrapView];
    _wrapView.backgroundColor = [UIColor orangeColor];
    _wrapView.contentMode = QGVAPWrapViewContentModeScaleToFill;
    _wrapView.autoDestoryAfterFinish = NO;
    _wrapView.hwd_Delegate = self;
//    [_wrapView setMute:YES];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ziweixing" ofType:@"mp4"];
    [_wrapView playHWDMP4:filePath repeatCount:0 delegate:self];
    
    UIButton *playView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:playView];
    playView.frame = CGRectMake(100, 100, 100, 60);
    [playView addTarget:self action:@selector(playMP4Method) forControlEvents:UIControlEventTouchUpInside];
    [playView setTitle:@"播放" forState:UIControlStateNormal];
    [playView setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    
    UIButton *voiceView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:voiceView];
    voiceView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 100, 150, 100, 60);
    [voiceView addTarget:self action:@selector(changeVoiceMethod:) forControlEvents:UIControlEventTouchUpInside];
    [voiceView setImage:[UIImage imageNamed:@"lucky_voice_open"] forState:UIControlStateNormal];
    [voiceView setImage:[UIImage imageNamed:@"lucky_voice_close"] forState:UIControlStateSelected];
    [playView setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
}

-(void)changeVoiceMethod:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        NSLog(@"静音");
        [self.wrapView setMute:YES];
    } else {
        NSLog(@"播放");
        [self.wrapView setMute:NO];
    }
}

-(void)playMP4Method {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ziweixing" ofType:@"mp4"];
    [_wrapView playHWDMP4:filePath repeatCount:0 delegate:self];
}

- (void)viewDidFinishPlayMP4:(NSInteger)totalFrameCount view:(VAPView *)container {
    NSLog(@"viewDidFinishPlayMP4:view:");
//    [self playMP4Method];
}

- (void)viewDidStartPlayMP4:(VAPView *)container {
    NSLog(@"viewDidStartPlayMP4:");
}
- (void)viewDidPlayMP4AtFrame:(QGMP4AnimatedImageFrame*)frame view:(VAPView *)container {
    NSLog(@"viewDidPlayMP4AtFrame:view:");
    
}
- (void)viewDidStopPlayMP4:(NSInteger)lastFrameIndex view:(VAPView *)container {
    NSLog(@"viewDidStopPlayMP4:view:");
    
}


- (void)vapWrap_viewDidStartPlayMP4:(VAPView *)container {
    NSLog(@"vapWrap_viewDidStartPlayMP4");
}
- (void)vapWrap_viewDidPlayMP4AtFrame:(QGMP4AnimatedImageFrame*)frame view:(VAPView *)container {
    NSLog(@"vapWrap_viewDidPlayMP4AtFrame");
}
- (void)vapWrap_viewDidStopPlayMP4:(NSInteger)lastFrameIndex view:(VAPView *)container {
    NSLog(@"vapWrap_viewDidStopPlayMP4");
}
- (void)vapWrap_viewDidFinishPlayMP4:(NSInteger)totalFrameCount view:(VAPView *)container {
    NSLog(@"vapWrap_viewDidFinishPlayMP4,  %@",[NSThread currentThread]);
    dispatch_async(dispatch_get_main_queue(), ^{
        [self playMP4Method];
    });
    
}
- (void)vapWrap_viewDidFailPlayMP4:(NSError *)error {
    NSLog(@"vapWrap_viewDidFailPlayMP4");
}


@end
